package com.spring.study.bean.definition;

import com.spring.study.domain.User;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * 注解构造器
 */
public class AnnotationDependencyConstructorInjectionDemo {


    public static void main(String[] args) {
        // 创建 BeanFactory 容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        // 注册 Configuration Class（配置类）
        //这种就类似于在类上加上@Configuration或@Component,这是我的理解
        applicationContext.register(AnnotationDependencyConstructorInjectionDemo.class);

        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(applicationContext);

        String xmlPath = "classpath:/META-INF/dependency-lookup-context.xml";
        //加载xml资源，解析并生成beanDefinition
        beanDefinitionReader.loadBeanDefinitions(xmlPath);

        // 启动 Spring 应用上下文
        applicationContext.refresh();

        //依赖查找并创建bean
        UserHolder userHolder = applicationContext.getBean(UserHolder.class);
        System.out.println(userHolder);

        // 显示地关闭 Spring 应用上下文
        applicationContext.close();
    }


    @Bean
    public UserHolder userHolder (User user) {
        return  new UserHolder(user);
    }




}
